//
//  GWYTabBar.swift
//  swift-GWYWeiBo
//
//  Created by x on 2017/3/10.
//  Copyright © 2017年 HLB. All rights reserved.
//

import UIKit

class GWYTabBar: UITabBar {
    
    let pulishBtu = UIButton.init()
    //从写initwithframe方法  调用init方法  最后也会调用initwithframe方法
    override init(frame: CGRect) {
        super.init(frame: frame)//调用父类初始化方法
        //创建TabBar中间的按钮 然后添加下
        pulishBtu.setImage(UIImage.init(named: "tabbar_compose_icon_add"), for: UIControlState.normal)
        pulishBtu.setImage(UIImage.init(named: "tabbar_compose_icon_add_highlighted"), for: UIControlState.highlighted)
        pulishBtu.setBackgroundImage(UIImage.init(named: "tabbar_compose_button"), for: UIControlState.normal)
        pulishBtu.setBackgroundImage(UIImage.init(named: "tabbar_compose_button_highlighted"), for: .highlighted)
        addSubview(pulishBtu);
    }
    
    //必须实现这个方法 (重写父类 构造器方法)
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //重写布局方法 layoutSubviews
    override func layoutSubviews() {
        super.layoutSubviews()
        
        
        var index = 0
        pulishBtu.frame.size = CGSize.init(width: bounds.size.width/5, height: bounds.size.height)
        pulishBtu.center = CGPoint.init(x: bounds.size.width/2, y: bounds.size.height/2)
        for btu in subviews {
            //判断是不是按钮  UIControl.classForCoder()创建一个类对象
            if !btu.isKind(of: UIControl.classForCoder()) || btu == pulishBtu {
                continue
            }
            //设置其他item的位置
            print("woqu ",index)
            btu.frame = CGRect.init(x: bounds.size.width/5 * CGFloat(index), y: 0, width: bounds.size.width/5, height: bounds.size.height)
            if index == 1 {
                index += 1
            }
            index += 1
        }
    }
}
